FROM php:7.4-fpm-alpine

# https://pkgs.alpinelinux.org/packages

# Mirror mirror switch to Ali-OSM (Alibaba Open Source Mirror Site) - http://mirrors.aliyun.com/
RUN echo 'http://mirrors.aliyun.com/alpine/latest-stable/main' > /etc/apk/repositories \
	&& echo '@community http://mirrors.aliyun.com/alpine/latest-stable/community' >> /etc/apk/repositories \
	&& echo '@testing http://mirrors.aliyun.com/alpine/edge/testing' >> /etc/apk/repositories

# https://github.com/matriphe/docker-alpine-php/blob/master/7.0/FPM/Dockerfile
# Environments
ENV TIMEZONE            Asia/Shanghai
ENV PHP_MEMORY_LIMIT    512M
ENV MAX_UPLOAD          50M
ENV PHP_MAX_FILE_UPLOAD 200
ENV PHP_MAX_POST        100M
ENV COMPOSER_ALLOW_SUPERUSER 1

RUN apk update \
	# && apk upgrade \
	&& apk add \
	# 		# vim \
	wget \
	tzdata \
	&& cp /usr/share/zoneinfo/${TIMEZONE} /etc/localtime \
	&& echo "${TIMEZONE}" > /etc/timezone \
	&& apk del tzdata \
	&& rm -rf /var/cache/apk/*
RUN apk add --no-cache --repository http://mirrors.aliyun.com/alpine/edge/community zlib-dev libpng-dev \
	libzip-dev \
	libmcrypt-dev \
	zstd-dev
# unixodbc-dev \
# php7-pdo_odbc@community \
RUN  docker-php-ext-install mysqli pdo_mysql exif \
	zip gd \
	opcache 
RUN apk add --no-cache autoconf g++ libtool make  \
	&& pecl install mcrypt \
	&& pecl install lzf igbinary redis
RUN docker-php-ext-enable lzf igbinary redis \
	mcrypt


# https://github.com/docker-library/php/issues/240
# https://gist.github.com/guillemcanal/be3db96d3caa315b4e2b8259cab7d07e
# https://forum.alpinelinux.org/forum/installation/php-iconv-issue

RUN apk add --no-cache --repository http://mirrors.aliyun.com/alpine/edge/community gnu-libiconv
ENV LD_PRELOAD /usr/lib/preloadable_libiconv.so php
RUN rm -rf /var/cache/apk/*

# # Production container
# RUN mv $PHP_INI_DIR/php.ini-production $PHP_INI_DIR/php.ini
# Development container
RUN mv $PHP_INI_DIR/php.ini-development $PHP_INI_DIR/php.ini
# Set environments
RUN sed -i "s|;*date.timezone =.*|date.timezone = ${TIMEZONE}|i" $PHP_INI_DIR/php.ini && \
	sed -i "s|;*memory_limit =.*|memory_limit = ${PHP_MEMORY_LIMIT}|i" $PHP_INI_DIR/php.ini && \
	sed -i "s|;*upload_max_filesize =.*|upload_max_filesize = ${MAX_UPLOAD}|i" $PHP_INI_DIR/php.ini && \
	sed -i "s|;*max_file_uploads =.*|max_file_uploads = ${PHP_MAX_FILE_UPLOAD}|i" $PHP_INI_DIR/php.ini && \
	sed -i "s|;*post_max_size =.*|post_max_size = ${PHP_MAX_POST}|i" $PHP_INI_DIR/php.ini && \
	sed -i "s|;*cgi.fix_pathinfo=.*|cgi.fix_pathinfo= 0|i" $PHP_INI_DIR/php.ini

# composer
RUN wget https://mirrors.aliyun.com/composer/composer.phar
RUN chmod +x composer.phar && mv composer.phar /usr/local/bin/composer

# RUN curl -sS https://getcomposer.org/installer | \
#     php -- --install-dir=/usr/bin/ --filename=composer

# composer laravel mirror

RUN composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/

RUN mkdir -p /usr/share/nginx/html
RUN mkdir -p /usr/local/var/log/php7
RUN mkdir -p /usr/local/var/run

COPY ./php-fpm.conf /usr/local/etc/

COPY ./www.conf /etc/php7/php-fpm.d/

# Expose volumes
VOLUME ["/usr/share/nginx/html", "/usr/local/var/log/php7", "/var/run/"]

WORKDIR /usr/share/nginx/html

EXPOSE 9000

# CMD ["/usr/sbin/php-fpm7", "-R", "--nodaemonize"]